package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.sl.au.entity.Account;
import com.sl.au.entity.Module;
import com.sl.au.entity.QuoteDevice;
import com.sl.au.entity.QuoteReplacement;
import com.sl.au.entity.User;
import com.sl.au.service.QuoteForeignService;
import com.sl.au.service.QuoteSearchService;
import com.sl.au.service.QuoteService;

@RestController
@RequestMapping(value = "/ui/quotesearch")
public class QuoteSearchController extends BaseController {
	@Autowired
	QuoteService quoteService;

	@Autowired
	QuoteSearchService quoteSearchService;

	@Autowired
	QuoteForeignService quoteForeignService;

	// 产品历史报价
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = createLayoutView("quote-search/quote-search-index");
		Account a = this.getMyAccount();
		User user = a.getUser();
		modelAndView.addObject("user", user);
		Module module = this.getPageModule("quotePumpMng");
		modelAndView.addObject("module", module);
		return modelAndView;
	}

	// 获得泵的历史价格的列表
	@RequestMapping(value = "/getpumgpage", method = RequestMethod.GET)
	public Map<String, Object> getPumgPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> qMap = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		Module module = this.getPageModule("quotePumpMng");
		String dataFilter = module.getDataFilter();
		Account a = this.getMyAccount();
		User user = a.getUser();
		Map<String, Object> condition = new HashMap<String, Object>();
		this.getPumpQueryParams(qMap);
		if (StringUtils.hasText(request.getParameter("userName"))) {
			condition.put("userName", request.getParameter("userName"));
		} else {
			condition.put("userName", null);
		}
		if (StringUtils.hasText(request.getParameter("projectName"))) {
			condition.put("projectName", request.getParameter("projectName"));
		} else {
			condition.put("projectName", null);
		}
		if (StringUtils.hasText(request.getParameter("customerName"))) {
			condition.put("customerName", request.getParameter("customerName"));
		} else {
			condition.put("customerName", null);
		}
		if (StringUtils.hasText(request.getParameter("superiorArea"))) {
			condition.put("superiorArea", request.getParameter("superiorArea"));
		} else {
			condition.put("superiorArea", null);
		}
		condition.put("key", key);
		condition.put("dataFilter", dataFilter);
		condition.put("qMap", qMap);
		List<QuoteDevice> quotes = quoteSearchService.getQueryQuoteDevicesHql(start, length, condition, user,
				"createTime", "desc");
		long count = quoteSearchService.getQueryQuoteDevicesHqlCount(condition, user);
		map.put("data", quotes);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	private void getPumpQueryParams(Map<String, Object> qMap) {
		// TODO Auto-generated method stub
		String names[] = { "productName", "productType", "productModel" };
		for (String name : names)
			if (StringUtils.hasText(request.getParameter(name))) {
				qMap.put(name, request.getParameter(name));
			}

		if (null != this.getDateQueryItem("createTime"))
			qMap.put("createTime", this.getDateQueryItem("createTime"));
		System.out.println(qMap.get("createTime"));
		
		if(null != this.getNumQueryItem("unitPrice")){
			qMap.put("unitPrice", this.getNumQueryItem("unitPrice"));
		}

		if(null != this.getIntegerQueryItem("number")){
			qMap.put("number", this.getIntegerQueryItem("number"));
			System.out.println(qMap.get("number"));
		}
	}

	// 获得备件的历史价格的列表
	@RequestMapping(value = "/getreplacepage", method = RequestMethod.GET)
	public Map<String, Object> getReplacePage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> qMap = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		Module module = this.getPageModule("quotePumpMng");
		String dataFilter = module.getDataFilter();
		Account a = this.getMyAccount();
		User user = a.getUser();
		System.out.println(qMap);
		this.getReplacementQueryParams(qMap);
		if (StringUtils.hasText(request.getParameter("userName"))) {
			condition.put("userName", request.getParameter("userName"));
		} else {
			condition.put("userName", null);
		}
		if (StringUtils.hasText(request.getParameter("projectName"))) {
			condition.put("projectName", request.getParameter("projectName"));
		} else {
			condition.put("projectName", null);
		}
		if (StringUtils.hasText(request.getParameter("customerName"))) {
			condition.put("customerName", request.getParameter("customerName"));
		} else {
			condition.put("customerName", null);
		}
		if (StringUtils.hasText(request.getParameter("superiorArea"))) {
			condition.put("superiorArea", request.getParameter("superiorArea"));
		} else {
			condition.put("superiorArea", null);
		}
		condition.put("key", key);
		condition.put("dataFilter", dataFilter);
		condition.put("qMap", qMap);
		List<QuoteReplacement> quotes = quoteSearchService.getQueryQuoteReplacementsHql(start, length, condition, user,
				"createTime", "desc");
		long count = quoteSearchService.getQueryQuoteReplacementsHqlCount(condition, user);
		map.put("data", quotes);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	private void getReplacementQueryParams(Map<String, Object> qMap) {
		// TODO Auto-generated method stub
		String names[] = { "deviceName", "type", "material" };
		for (String name : names)
			if (StringUtils.hasText(request.getParameter(name))) {
				qMap.put(name, request.getParameter(name));
			}
		
		if (null != this.getDateQueryItem("createTime")){
			qMap.put("createTime", this.getDateQueryItem("createTime"));
		}

		if(null != this.getNumQueryItem("price")){
			qMap.put("price", this.getNumQueryItem("price"));
			System.out.println(this.getNumQueryItem("price"));
		}
		
		if(null != this.getIntegerQueryItem("amount")){
			qMap.put("amount", this.getIntegerQueryItem("amount"));
		}
	}

}
